﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UIT_CV_Management
{
    public class Settings
    {
        public String Language;
        public Settings()
        {
            QueryResult result = Database.shareDatabase
                .Query("SELECT Value FROM Settings WHERE Param='Language'");

            if (result.ReturnCode == 200)
                //OK
                Language = UIT_CV_Management.Language.LanguageName(result.Table.Rows[0][0].ToString());
            else
            {
                //Create current language
                Language = UIT_CV_Management.Language.LanguageName("en");
                this.Save();
            }
        }

        public Boolean Save()
        {
            UIT_CV_Management.Language.CurrentLanguage = UIT_CV_Management.Language.LanguageCode(Language);
            QueryResult result = Database.shareDatabase
                .Query("SELECT Value FROM Settings WHERE Param='Language'");

            if (result.ReturnCode == 200)
            {
                //Update
                QueryResult _result = Database.shareDatabase
                    .Query("UPDATE Settings SET Value='" +
                    UIT_CV_Management.Language.LanguageCode(Language) +
                    "' WHERE Param='Language'");
                return _result.ReturnCode == 200;
            }
            else
            {
                //Insert new one
                String SettingID = Database.getID("Settings", "ID");
                QueryResult _result = Database.shareDatabase
                    .Query("INSERT INTO Settings VALUE('"+SettingID+"', 'Language', '" + 
                    UIT_CV_Management.Language.LanguageCode(Language) + "')");
                return _result.ReturnCode == 200;
            }
        }
    }
}
